﻿namespace EasyWeb
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using EasyWeb.Data;
    using EasyWeb.Design;
    using EasyWeb.Validation;

    [EWDisplayName(Name = "FieldDefinitionFile", ResourceType = typeof(EWFieldDefinitionResources))]
    public class EWFieldDefinitionFile : EWFieldDefinition
    {
        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowDefaultValue")]
        public override bool AllowDefaultValue
        {
            get { return false; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowUnique")]
        public override bool AllowUnique
        {
            get { return false; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_DefaultValue")]
        public override object DefaultValue
        {
            get { throw new NotSupportedException(); }
            set { throw new NotSupportedException(); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionFile_MinimumLength")]
        [EWPropertyCompareValidation("MaximumLength", EWCompareMode.LessThanOrEquals)]
        public long? MinimumLength
        {
            get { return this.StateBag.Get<long?>("MinimumLength"); }
            set { this.StateBag.Set("MinimumLength", value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionFile_MaximumLength")]
        public long? MaximumLength
        {
            get { return this.StateBag.Get<long?>("MaximumLength"); }
            set { this.StateBag.Set("MaximumLength", value); }
        }

        public string FileNameColumnName
        {
            get { return string.Format("filename_{0}", this.InternalName); }
        }

        public string FileMimeColumnName
        {
            get { return string.Format("filemime_{0}", this.InternalName); }
        }

        public string FileBinaryColumnName
        {
            get { return string.Format("filebinary_{0}", this.InternalName); }
        }

        public override EWFieldDefinitionColumnCollection CreateFieldDefinitionColumns()
        {
            return new EWFieldDefinitionColumnCollection()
            {
                new EWFieldDefinitionColumn()
                {
                    Name = this.FileNameColumnName,
                    IsNullable = this.AllowNull,
                    IsUnique = this.AllowUnique && this.IsUnique,
                    Type = DbType.String,
                    Length = 256
                },
                new EWFieldDefinitionColumn()
                {
                    Name = this.FileMimeColumnName,
                    IsNullable = this.AllowNull,
                    IsUnique = this.AllowUnique && this.IsUnique,
                    Type = DbType.AnsiString,
                    Length = 64
                },
                new EWFieldDefinitionColumn()
                {
                    Name = this.FileBinaryColumnName,
                    IsNullable = this.AllowNull,
                    IsUnique = this.AllowUnique && this.IsUnique,
                    Type = DbType.Binary,
                    Length = int.MaxValue
                }
            };
        }

        public override void Deserialize(EWSerializationInfo info)
        {
            base.Deserialize(info);

            this.MaximumLength = info.GetNullableInt64("MaximumLength");
            this.MinimumLength = info.GetNullableInt64("MinimumLength");
        }

        public override void Serialize(EWSerializationInfo info)
        {
            base.Serialize(info);

            info.AddValue("MaximumLength", this.MaximumLength);
            info.AddValue("MinimumLength", this.MinimumLength);
        }
    }
}
